package com.wenhao.pool;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Wenhao Tong
 * @create 2021-08-20 15:54
 */
public class ExecutorsDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        for (int i = 0;i < 10;i++) {
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }
    }

    /**
     * 三大方法
     */
    @Test
    public void test1() {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        for (int i = 0;i < 10;i++) {
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }
    }

    @Test
    public void test2() {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        for (int i = 0;i < 10;i++) {
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }
    }
    @Test
    public void test3() {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0;i < 10;i++) {
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }
    }
}
